/**
 * @name zhangyy-g@grandsoft.com.cn
 * @description zhangyy's code
 * Date: 15-4-17
 * Time: 下午5:52
 *
 */
(function ($) {
    var docView = Backbone.View.extend({
        className: 'doc-item',
        events: {
            'click': 'itemClickHandler',
            'click .checkbox': 'checkboxHandler'
        },
        initialize: function () {
            var self = this
            self.listenTo(self.model, 'change', self.render)
        },
        render: function () {
            var self = this
            var data = self.model.toJSON()

            App.common.modules.util.setFileIcon(data)

            self.$el.html(Mustache.render(itemTpl, data))

            return self
        },
        itemClickHandler: function () {
            var self = this
            var data = null
            var fileJSON = data = self.model.toJSON()

            if (data.dir || data.isDir || data.isdir) {
                /*var filter = data.filter
                if(filter == 'search') {
                    var firstPath = data.filePath.split('/')[1]
                }*/
                $.jps.publish('file-list', {
                    path: data.path || data.filePath,
                    filter: data.filter
                })
            } else {
                var util = App.common.modules.util
                if(!util.canPreview(data)) {
                    App.common.modules.smallnote('权限不足', {
                        pattern: 'error'
                    })
                    return false
                }
                if (util.isImg(data)) {
                    //如果是图片，则在线预览
                    $.jps.publish('img-preview', data)
                } else if (util.isOffice(data)) {
                    $.jps.publish('office-preview', data)
                } else {
                    App.common.modules.smallnote('暂不支持该类型文件的预览', {
                        pattern: 'error'
                    })
                    return false
                    if(fileJSON.isLinkView){
                        alert('对不起，不支持预览！')
                        return false
                    }
                    $.ajax({
                        url: '/review/file/getdownloadinfo/v2/',
                        method: 'post',
                        dataType: 'json',
                        data: {
                            path: data.path
                        },
                        success: function (data) {
                            if(data.code !=0 ) return false
                            var url = data.data.downloadUrl
                            alert('下载链接：' + url)

                            cordova.exec(function () {
                                    //App.common.modules.smallnote('文件开始下载')
                                }, function (error) {
                                    App.common.modules.smallnote('对不起，下载失败', {
                                        pattern: 'error'
                                    })
                                },
                                'Download', 'downloadFile',
                                [{
                                    remote: url,
                                    path: ''
                                }]
                            );

                        }
                    })

                }
            }
        },

        checkboxHandler: function () {
            var self = this
            //self.$el.find('.checkbox').toggleClass('checked')

            self.model.set({
                'isChecked': !self.model.get('isChecked')
            })
            return false;
        }
    })

    var itemTpl = '' +
        '{{^isLinkView}}<span class="checkbox {{#isChecked}}checked{{/isChecked}}"></span>{{/isLinkView}}' +
        '<em class="ficon-{{fileIcon}}"></em>' +
        '<span class="filename">{{fileName}}</span>'
    App.modules.docView = docView
})(jQuery)